package com.jungle.sp.daily.feature.d8_structured_concurrency;

import lombok.SneakyThrows;

import java.util.concurrent.StructuredTaskScope;

/**
 * @author Jungle
 * @create 2024-07-14 10:04
 */
public class Demo {

    @SneakyThrows
    public static void main(String[] args) {
        EmpTask task1 = new EmpTask();
        DeptTask task2 = new DeptTask();
        // 有一个执行失败了其他的都不执行
        try (StructuredTaskScope.ShutdownOnFailure scope = new StructuredTaskScope.ShutdownOnFailure()) {
        // 一个执行成功时候关闭其他的
        // try (StructuredTaskScope.ShutdownOnSuccess scope = new StructuredTaskScope.ShutdownOnSuccess<>()) {
            StructuredTaskScope.Subtask<String> subtask1 = scope.fork(task1);
            StructuredTaskScope.Subtask<String> subtask2 = scope.fork(task2);
            scope.join();
        //    获取两个任务的结果
            String s = subtask1.get();
            String s2 = subtask2.get();
            System.out.println(STR."\{s} \{s2}");
        }
    }
}
